<?php

namespace app\admin\server;

use app\common\server\Caches;
use app\common\server\Tree;
use think\Cache;

class Menu
{
    //配置
    protected static $config = [
        'menu' => 'menu',
    ];

    //获取菜单所有数据
    public static function get()
    {
        return Caches::getCache(
            self::$config['menu'],
            0,
            [
                'class' => ['\app\admin\model\apower', 'getDatas'],
                'param' => [],
            ]
        );
    }

    //获取面包屑导航
    public static function getTitle($id, $level = 1)
    {
        $html = '';
        $info = self::getOne($id);
        if (!empty($info)) {
            $html .= self::getTitle($info['columnid'], $level + 1);
            if ($level == 1) {
                $html .= '<a><cite>' . $info['powername'] . '</cite></a>';
            } else {
                $html .= '<a href="' . url($info['controller']) . '">' . $info['powername'] . '</a>';
            }
        }
        return $html;
    }

    //获取节点相关
    public static function getNode($parentId = 0, $level = 1, $checked = [], $other = [])
    {
        $arr = [];
        $data = self::get();
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                if ($v['columnid'] == $parentId) {

                    $color = ($v['status'] == 0) ? '#2F4056' : '#e2e2e2';
                    $title = ($v['status'] == 0) ? 'title="' . $v['describe'] . '"' : 'title="禁用"';
                    $startStr = '<i class="icon iconfont">' . (($v['type'] == 1) ? '' : '') . '</i>';

                    $title = '<span style="color:' . $color . '" ' . $title . '>' . $startStr . $v['powername'] . '</span>';

                    $val = [
                        'id' => $v['powerid'],
                        'title' => $title,
                        'field' => 'powername',
                        'spread' => true,
                        //'href' => url($v['controller']),
                        'checked' => in_array($v['powerid'], $checked) ? true : false,
                        'children' => self::getNode($v['powerid'], $level + 1, $checked, $other),
                    ];
                    if (!empty($other)) $val = array_merge($val, $other);

                    $arr[] = $val;
                }
            }
        }
        return $arr;
    }

    //获取单个节点信息
    public static function getOne($id)
    {
        $data = self::get();
        return isset($data[$id]) ? $data[$id] : [];
    }

    //获取后台左侧菜单
    public static function getMenu($parentId = 0, $level = 1, $powerid = [])
    {
        $html = '';
        $data = self::get();

        if (!empty($data)) {
            foreach ($data as $k => $v) {

                if (!empty($powerid) && !in_array($v['powerid'], $powerid)) continue;

                if (($v['type'] == 1) && ($v['status'] == 0) && ($v['columnid'] == $parentId)) {

                    $html .= self::html($v, $level, $powerid);
                }
            }
        }
        return $html;
    }

    protected static function html($row, $level, $powerid)
    {
        $html = '';
        if ($level == 1) {
            $html .= '<li data-name="' . $row['powername'] . '" class="layui-nav-item layui-nav-itemed">' .
                '         <a href="javascript:;" lay-tips="' . $row['powername'] . '" lay-direction="2">
                             <i class="icon iconfont"> ' . $row['class'] . '</i>
                             <cite>' . $row['powername'] . '</cite>
                         </a>';

            if (self::hasChild($row['powerid'])) {
                $html .= '<dl class="layui-nav-child">' .
                    self::getMenu($row['powerid'], $level + 1, $powerid) .
                    '</dl>';
            }
            $html .= '</li>';

        } elseif ($level > 1) {
            if (self::hasChild($row['powerid'])) {
                $html .= '<dd data-name="' . $row['powername'] . '">' .
                    '<a href="javascript:;"><i class="icon iconfont"> ' . $row['class'] . '</i>' . $row['powername'] . '<span class="layui-nav-more"></span></a>' .
                    '<dl class="layui-nav-child">' .
                    self::getMenu($row['powerid'], $level + 1, $powerid) .
                    '</dl>' .
                    '</dd>';

            } else {
                $html .= '<dd data-name="' . $row['powername'] . '">
                              <a lay-href="' . url($row['controller'], ['powerid' => $row['powerid']]) . '"><i class="icon iconfont"> ' . $row['class'] . '</i>' . $row['powername'] . '</a>
                          </dd>';
            }
        }

        return $html;
    }

    //循环拼接出html的菜单列表....
    protected static function hasChild($parentId)
    {
        $result = false;
        $data = self::get();
        foreach ($data as $k => $v) {
            if (($v['type'] == 1) && ($v['status'] == 0) && ($v['columnid'] == $parentId)) {
                $result = true;
                break;
            }
        }
        return $result;
    }

    //获取适合layui的树状图
    public static function getNodeTree()
    {
        $data = self::get();
        $data[0] = ['powerid' => 0, 'columnid' => '-1', 'powername' => '顶级'];

        Tree::init(['parentName' => 'columnid', 'correlation' => 'powerid'], $data);
        return Tree::toTreeAppose(-1, 1, 'powername');
    }

    //清除所有菜单相关的缓存
    public static function clear()
    {
        foreach (self::$config as $k => $v) {
            Cache::rm($v);
        }
    }

    public static function checkPower($power, $action)
    {
        $allow = false;
        $data = self::get();
        $arr = array_intersect_key($data, array_flip($power));
        foreach ($arr as $k => $v) {
            if ($v['controller'] == $action) {
                $allow = true;
                break;
            }
        }
        return $allow;
    }
}